package com.et.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * 导入Excel公共类
 * @author Administrator
 *
 */
public class OutExcel {
	public static void toExcel(HttpServletResponse response,String name,String[] title,List list,String type){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
		   String fileSrt = name+".xls";
		   File file = new File(fileSrt);
		   WritableWorkbook wwb = Workbook.createWorkbook(file);
		   // 添加第一个工作表并设置第一个Sheet的名字   
		   WritableSheet sheet = wwb.createSheet(name, 0);  
		   WritableFont font = new WritableFont(WritableFont.createFont("宋体"),10,WritableFont.BOLD); 
		   WritableCellFormat wc = new WritableCellFormat(font);
		   wc.setBorder(Border.ALL, BorderLineStyle.THIN);
		   wc.setAlignment(Alignment.CENTRE);
		   WritableCellFormat wcc = new WritableCellFormat();
		   wcc.setBorder(Border.ALL, BorderLineStyle.THIN);
		   Label label;   
		   sheet.mergeCells(0,0,title.length-1,0);
		   label = new Label(0,0,name,wc);
		   sheet.addCell(label);
		   //循环标题
		   for(int i=0;i<title.length;i++){
			   label = new Label(i,1,title[i],wc);   
		   	   sheet.addCell(label);
		   }
		   /**循环数据
		   if(type.equals("OrderInfo")){
			   for(int i=0;i<list.size(); i++){
				   OrderInfo orderInfo = (OrderInfo)list.get(i);
				   label = new Label(0,i+2,orderInfo.getCustomerInfo().getRealName(),wcc);
				   if(orderInfo.getCategoryID()==CardInfoSet.cardCategory_MONTH){
					   sheet.addCell(label);
					   label = new Label(1,i+2,orderInfo.getMonthInfo().getMonthName(),wcc);
				   }else if(orderInfo.getCategoryID()==CardInfoSet.cardCategory_TERMINFO){
					   sheet.addCell(label);
					   label = new Label(1,i+2,orderInfo.getTermInfo().getTermName(),wcc);
				   }
				   sheet.addCell(label);
				   label = new Label(2,i+2,orderInfo.getProductID()==CardInfoSet.cardCategory_TERMINFO?"期卡":"月卡",wcc);
				   sheet.addCell(label);
				   label = new Label(3,i+2,"" +orderInfo.getPrice() + "",wcc);
				   sheet.addCell(label);
				   label = new Label(4,i+2,"" +orderInfo.getSalePrice() + "",wcc);
				   sheet.addCell(label);
				   label = new Label(5,i+2,orderInfo.getSaleCategory()==saleCategory.card_saleCategory?"点卡":"直冲",wcc);
				   sheet.addCell(label);
				   label = new Label(6,i+2,"" +orderInfo.getCardID() + "",wcc);
				   sheet.addCell(label);
				   label = new Label(7,i+2,"" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(orderInfo.getCreateDate()) + "",wcc);
				   sheet.addCell(label);
			   }
		   }**/
		   wwb.write();
		   wwb.close();
		  
		   FileInputStream f = new FileInputStream(file);
	       byte[] fb = new byte[f.available()];
	       f.read(fb);
	       response.setHeader("Content-disposition", "attachment; filename="+new String(fileSrt.getBytes("gb2312"),"iso8859-1"));
	       ByteArrayInputStream bais = new ByteArrayInputStream(fb);
	       int b;
	       while ((b = bais.read()) != -1 ) {
	          response.getOutputStream().write(b);
	       }
	       response.getOutputStream().flush();
	   }catch (Exception e) {
		   System.out.println("---导出Excel出现异常---");
		   e.printStackTrace();   
	   }
	}
}
